package com.evil.common.oss.service.storage;

import cn.hutool.core.date.DateUtil;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.file.enums.FileServiceProviderEnum;
import com.evil.common.oss.enums.OssRCodeEnum;
import com.evil.common.oss.props.LocalOssProperty;
import com.evil.common.oss.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;

/**
 * 本地文件OSS
 */
@Slf4j
public class LocalOssFileStorageService extends FileStorageService<LocalOssProperty> {

    public LocalOssFileStorageService(LocalOssProperty property) {
        super(property);
    }

    @Override
    public FileServiceProviderEnum getServiceProvider() {
        return FileServiceProviderEnum.LOCAL;
    }

    @Override
    public String getFileUrl(String path) {
        return this.getPreview() + path;
    }

    @Override
    public String getDownloadUrl(String path, String fileName) {
        // 本地环境需要凭借文件名参数
        return this.getPreview() + path + "?fileName=" + fileName;
    }

    @Override
    public String upload(byte[] data, String path, String sourceFileName, String contentType, boolean isPreview) {
        return upload(new ByteArrayInputStream(data), path, sourceFileName, contentType, isPreview);
    }

    @Override
    public String upload(InputStream inputStream, String path, String sourceFileName, String contentType, boolean isPreview) {
        if (inputStream == null) {
            return null;
        }
        // 获取文件名
        String[] paths = path.split("/");
        if (!paths[paths.length - 1].contains(".")) {
            throw new BusinessException(OssRCodeEnum.ERROR_FILE_NOT_FOUND);
        }
        String fileName = paths[paths.length - 1];
        path = StringUtils.join(Arrays.copyOf(paths, paths.length - 1), "/");
        String filePath = super.getProperty().getSavePath() + "/" + path;
        try {
            FileUtil.writeFile(filePath, fileName, inputStream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(OssRCodeEnum.UPLOAD_ERROR, OssRCodeEnum.UPLOAD_ERROR.getMessage() + e.getMessage());
        }
        return super.getProperty().getVisitPath() + "/" + path + "/" + fileName;
    }

    @Override
    public File getFileByFilePath(String filePath, File file) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        filePath = super.getProperty().getSavePath() + "/" + filePath;
        try {
            return new File(filePath);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(OssRCodeEnum.FILE_DOWNLOAD_ERROR, OssRCodeEnum.FILE_DOWNLOAD_ERROR.getMessage() + e.getMessage());
        }
    }

    @Override
    public URL getDownloadUrl(String filePath, String fileName, Date expirationTime) {
        if (DateUtil.compare(new Date(), expirationTime) > 0) {
            throw new BusinessException(OssRCodeEnum.FILE_OVERDUE);
        }
        try {
            return new URL(this.getDownloadUrl(filePath, fileName));
        } catch (MalformedURLException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(OssRCodeEnum.FILE_DOWNLOAD_ERROR, OssRCodeEnum.FILE_DOWNLOAD_ERROR.getMessage() + e.getMessage());
        }
    }
}
